package com.localization;

import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/**
 * Created by Edward on 2017/1/6.
 */
public class Gateway {

    private boolean working = false;
    private Selector selector;
    public HashMap objectlist = new HashMap();
    public static HashMap<String,SocketClientInterface > MacSocke=new HashMap<String,SocketClientInterface >();
    public  static ArrayList<SocketClientInterface> msocket=new ArrayList<SocketClientInterface>();

    public Gateway()
    {
        System.out.println("Gateway");
        try {
            selector = Selector.open();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Selector getSelector()
    {
        System.out.println("Selector");
        return this.selector;
    }

    public void register(String MAC,Object channel,SocketClientInterface objs)
    {
        objectlist.put(channel,objs);

        msocket.add(objs);
        MacSocke.put(MAC,objs);
    }

    public void working()
    {
        System.out.println("working");
        working = true;
        new Thread(new ClientThread()).start();
    }

    public void close()
    {
        working = false;
        try {
            if(selector.isOpen())
                selector.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        objectlist.clear();
    }

    private class ClientThread implements Runnable
    {
        @Override
        public void run()
        {
            try{
                while(working) {
//                    System.out.println("ClientThread");
                    int cnt = selector.select(3000);
                    if(cnt == 0)
                        continue;
//                    System.out.println("selector.select(3000) > 0");
                    Iterator KeyIterator = selector.selectedKeys().iterator();
                    while(KeyIterator.hasNext())
                    {
                        SelectionKey mSelectionKey = (SelectionKey) KeyIterator.next();
                        if(mSelectionKey.isReadable())
                        {
//                          /*调用回调函数*/
                            SocketClientInterface objs = (SocketClientInterface) objectlist.get(mSelectionKey.channel());
                            objs.Process(mSelectionKey);
                            /*完成TCP连接建立请求，否则在写入时会出现
                            * java.nio.channels.NotYetConnectedException
                            */
                        }else if(mSelectionKey.isConnectable()) {
                            SocketChannel client = (SocketChannel) mSelectionKey.channel();
                            if(client.isConnectionPending())
                            {
                                client.finishConnect();
                                System.out.println("Connect finish");
                            }
                        }
                        KeyIterator.remove();
                    }
                }
            }catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}
